// pages/person/funs/funs.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')
var currentIndex = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cellsOne: [],
    cellsTwo: [],
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.requestList();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1
    });
    this.requestList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    });
    this.requestList();
  },
  //进入发布者个人页面
  actionFriend(event) {
    wx.navigateTo({
      url: '/pages/friend/friend?userId='+event.currentTarget.dataset.userid,
    })
  },
   //头像加载错误处理
   headerImageLoadFail() {
    this.setData({
      headerUrl: "/images/person/defaultHeader.png"
    })
  },
//标签切换
onChange(event) {
  currentIndex = event.detail.index;
  this.setData({
    page: 1
  });
  console.log('============='+currentIndex);
  this.requestList();
},
//请求列表
requestList() {
  var url = '/user/attention/' + wx.getStorageSync('USERDATA').platformId;
  if (currentIndex == 1) {
    url = '/user/fan/' + wx.getStorageSync('USERDATA').platformId;
  }
  let obj = {
    method: 'GET',
    url: url,
    showLoading: true,
    data: {
      page: this.data.page,
      size: 20,
      fromPlatformId: wx.getStorageSync('USERDATA').platformId
    }
  }
  httpUtils.request(obj).then(res => {
    if (currentIndex == 1) {
      if (this.data.page == 1) {
        this.setData({
          cellsTwo: []
        });
      }
      const {
        cellsTwo = []
      } = this.data;
      res.data.attentionDetailDtos.forEach(function (item, index) {
        cellsTwo.push(item);
      });
      this.setData({
        cellsTwo
      });
    } else {
      if (this.data.page == 1) {
        this.setData({
          cellsOne: []
        });
      }
      const {
        cellsOne = []
      } = this.data;
      res.data.attentionDetailDtos.forEach(function (item, index) {
        cellsOne.push(item);
      });
      this.setData({
        cellsOne
      });
    }
    
  }).catch(err => {
    console.log(err)
  });
}
})